using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEngine.EventSystems;

public class TrackItem : MonoBehaviour
{
    public ExecuteClipType SkillClipType => SkillClipData.ExecuteClipType;

    public RectTransform ClipRect;
    public Image ClipRectImage;
    public Color ClipRectColor = Color.white;
    public Color ExecutionClipRectColor = Color.white;
    public Color ParticleEffectRectColor = Color.white;
    [Space(10)]
    public Image ClipTypeBar;
    public Color AnimationClipBarColor = Color.white;
    public Color AudioClipBarColor = Color.white;
    public Color ExecutionClipBarColor = Color.white;
    public Color ParticleEffectBarColor = Color.white;
    [Space(10)]
    public Slider SliderLeft;
    public Slider SliderRight;
    public GameObject ActionImg1;
    public GameObject ActionImg2;
    public RectTransform LeftLine;

    private int panelWidth;
    public SkillClipData SkillClipData;
    public Action OnEndDrag;
    public EventTrigger trigger;
    // Start is called before the first frame update
    void Start()
    {
        panelWidth = Screen.width - 40;
    }

    // Update is called once per frame
    void Update()
    {
        if(SkillClipData == null)
        {
            return;
        }

        if(SkillClipData.TotalTime <= 0)
        {
            return;
        }

        var timeData = SkillClipData.GetClipTime();
        timeData.StartTime = SliderLeft.value * SkillClipData.TotalTime;
        timeData.EndTime = SliderRight.value * SkillClipData.TotalTime;
        var s = ClipRect.sizeDelta;
        var x = SliderLeft.value * panelWidth;
        var y = SliderRight.value * panelWidth;
        var w = y - x;
        ClipRect.sizeDelta = new Vector2(w, s.y);
        ClipRect.anchoredPosition = new Vector2(SliderLeft.value * panelWidth, 0);
    }

    public void SetClipType(SkillClipData skillClipData)
    {
        SkillClipData = skillClipData;
        switch (skillClipData.ExecuteClipType)
        {
            case ExecuteClipType.ActionEvent:
                ActionImg1.SetActive(true);
                ActionImg2.SetActive(true);
                var s = ClipRect.sizeDelta;
                ClipRect.sizeDelta = new Vector2(20, s.y);
                SetDragEvent();
                return;
            case ExecuteClipType.Animation:
                ClipRectImage.color = ClipRectColor;
                ClipTypeBar.color = AnimationClipBarColor;
                break;
            case ExecuteClipType.Audio:
                ClipRectImage.color = AudioClipBarColor;
                ClipTypeBar.color = AudioClipBarColor;
                break;
            case ExecuteClipType.CollisionExecute:
                ClipRectImage.color = ExecutionClipRectColor;
                ClipTypeBar.color = ExecutionClipBarColor;

                var executeType = skillClipData.CollisionExecuteData.MoveType;
                if(executeType == CollisionMoveType.PathFly || executeType == CollisionMoveType.SelectedDirectionPathFly)
                {
                    // LeftLine.gameObject.SetActive(true);
                    gameObject.AddComponent<BezierComponent>().CollisionExecuteData = SkillClipData.CollisionExecuteData;
                }
                else
                {
                    LeftLine.gameObject.SetActive(false);
                }
                break;
            case ExecuteClipType.ParticleEffect:
                ClipRectImage.color = ParticleEffectRectColor;
                ClipTypeBar.color = ParticleEffectBarColor;
                break;
            default:
                break;
        }

        SliderRight.value = SkillClipData.GetClipTime().EndTime / SkillClipData.TotalTime;
        SliderLeft.value = SkillClipData.GetClipTime().StartTime / SkillClipData.TotalTime;
        SetDragEvent();
    }


    public void SetDragEvent()
    {
        var entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.PointerClick;
        entry.callback.AddListener(PointerClick);
        trigger.triggers.Add(entry);

        if(SkillClipType != ExecuteClipType.ActionEvent
            && SkillClipType != ExecuteClipType.CollisionExecute
            && SkillClipType != ExecuteClipType.ParticleEffect
            && SkillClipType != ExecuteClipType.Animation
        )
        {
            SliderLeft.enabled = false;
            SliderRight.enabled = false;
            if(SkillClipType != ExecuteClipType.Animation)
            {
                return;
            }
        }

        entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.BeginDrag;
        entry.callback.AddListener(BeginDrag);
        trigger.triggers.Add(entry);

        entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.Drag;
        entry.callback.AddListener(Drag);
        trigger.triggers.Add(entry);

        entry = new EventTrigger.Entry();
        entry.eventID = EventTriggerType.EndDrag;
        entry.callback.AddListener(EndDrag);
        trigger.triggers.Add(entry);

        SliderLeft.onValueChanged.AddListener(OnSliderLeftValueChanged);
        SliderRight.onValueChanged.AddListener(OnSliderRightValueChanged);
    }

    float oldLeftValue;
    void OnSliderLeftValueChanged(float value)
    {
        if((SliderLeft.value + 0.01f) > SliderRight.value)
        {
            SliderLeft.SetValueWithoutNotify(oldLeftValue);
            return;
        }
        oldLeftValue = value;
    }

    float oldRightValue;
    void OnSliderRightValueChanged(float value)
    {
        if((SliderRight.value + 0.01f) > SliderRight.value)
        {
            SliderRight.SetValueWithoutNotify(oldRightValue);
            return;
        }
        oldRightValue = value;
    }

    public void DisableSlider()
    {
        SliderLeft.handleRect.gameObject.SetActive(false);
        SliderRight.handleRect.gameObject.SetActive(false);
    }

    public void EnableSlider()
    {
        SliderLeft.handleRect.gameObject.SetActive(true);
        SliderRight.handleRect.gameObject.SetActive(true);
    }

    void PointerClick(BaseEventData data)
    {
#if UNITY_EDITOR
        // set current skill clip data
        //set
        // var ui = UIMgr.Instance.GetOpenUI("SkillEditor") as SkillEditorUI;
        //ui.SetCurrentSkillClipData(SkillClipData);
        var pEventData = (PointerEventData)data;
        if(pEventData.button == PointerEventData.InputButton.Left)
        {
            //open operate menu
            // ui.OpenOperateMenu(SkillClipData);
            if(SkillClipType == ExecuteClipType.CollisionExecute)
            {
                var executeType = SkillClipData.CollisionExecuteData.MoveType;
                if(executeType == CollisionMoveType.PathFly || executeType == CollisionMoveType.SelectedDirectionPathFly)
                {
                    Selection.activeObject = this;
                    EditorGUIUtility.PingObject(SkillClipData);
                }
                else
                {
                    // LeftLine.gameObject.SetActive(false);
                    Selection.activeObject = SkillClipData;
                    EditorGUIUtility.PingObject(SkillClipData);
                }
            }
            else
            {
                Selection.activeObject = SkillClipData;
                EditorGUIUtility.PingObject(SkillClipData);
            }

        }
        if(pEventData.button == PointerEventData.InputButton.Right)
        {
            //open operate menu
            // ui.OpenOperateMenu(SkillClipData);
            // ui.RightContextTrm.gameObject.SetActive(true);
            // ui.Instance.RightContextTrm.rectTransform().anchoredPosition = new Vector2(pEventData.position.x, pEventData.position.y - Screen.height);
        }

#endif
    }

    void BeginDrag(BaseEventData data)
    {
        LeftLine.gameObject.SetActive(true);
    }

    void Drag(BaseEventData data)
    {
        var x = (data as PointerEventData).delta.x;
        x = x / panelWidth;
        var minx = x;
        if(x < 0)
        {
            if(SliderLeft.value <= 0)
            {
                return;
            }
            else
            {
                minx = Mathf.Max(-SliderLeft.value, x);
            }
        }

        if(SkillClipType == ExecuteClipType.ActionEvent)
        {
            if(x > 0)
            {
                if(SliderLeft.value >= 1)
                {
                    return;
                }
                else
                {
                    minx = Mathf.Min(1 - SliderRight.value, x);
                }
            }
        }
        else
        {
            if(x > 0)
            {
                if(SliderRight.value >= 1)
                {
                    return;
                }
                else
                {
                    minx = Mathf.Min(1 - SliderRight.value, x);
                }
            }
        }

        SliderLeft.SetValueWithoutNotify(SliderLeft.value + minx);
        SliderRight.SetValueWithoutNotify(SliderRight.value + minx);

        var p = LeftLine.anchoredPosition3D;
        LeftLine.anchoredPosition3D = new Vector2(SliderLeft.value * panelWidth, p.y);
    }

    void EndDrag(BaseEventData data)
    {
        LeftLine.gameObject.SetActive(false);
        OnEndDrag?.Invoke();
    }
}
